/*
 $Id: huffman_internal_build_table.c,v 1.2 2005/06/05 11:27:39 olivier Exp $
 +----------------------------------------------------------------------+
 | Copyright : mFactories 2001, 2005 
 +----------------------------------------------------------------------+
 | Projet : DataStructure
 +----------------------------------------------------------------------+
 | $Source: /cvs/DataStructure/DataStructureLib/src/compress/huffman_internal_build_table.c,v $
 | $Author: olivier $
 | $Date: 2005/06/05 11:27:39 $
 | $Revision: 1.2 $ 
 +----------------------------------------------------------------------+
 | Description :                                              
                                        
 +----------------------------------------------------------------------+
 | Historique :
 $Log: huffman_internal_build_table.c,v $
 Revision 1.2  2005/06/05 11:27:39  olivier
 - Ajout de netinet/in.h pour linux (ntohl, htons etc...)

 Revision 1.1  2005/05/20 22:33:44  olivier
 Explosion des sources : 1 fichier = 1 fonction

 
 +----------------------------------------------------------------------+
*/

/**
 * Inclusions standards
 */
#include <ukapi.h>

/**
 * Inclusions locales
 */
#include "bit.h"
#include "binarytree.h"
#include "compress.h"
#include "priority_queue.h"

void huffman_internal_build_table( pbinarytree_node_t node, unsigned short code, unsigned char taille, HuffCode * table ) 
{
	if ( ! binarytree_is_eob( node ) ) 
	{
		if ( ! binarytree_is_eob( binarytree_left( node ) ) ) 
		{
			/* Part vers la left et ajoute 0 au node courant. */
			huffman_internal_build_table( binarytree_left( node ), code << 1, taille + 1, table );
		}

		if ( ! binarytree_is_eob( binarytree_right( node ) ) ) 
		{
			/* Part vers la right et ajoute 1 au node courant. */
			huffman_internal_build_table( binarytree_right( node ), ( code << 1 ) | 0x0001, taille + 1, table );
		}

		if ( binarytree_is_eob( binarytree_left( node ) ) && binarytree_is_eob( binarytree_right( node ) ) ) 
		{
			/* Assure que le code courant est au format big-endian. */
			code = htons( code );

			/* Assigne le code courant au symbole de la feuille. */
			table[((HuffNode *) binarytree_data( node ))->symbole].utilise	= 1;
			table[((HuffNode *) binarytree_data( node ))->symbole].code		= code;
			table[((HuffNode *) binarytree_data( node ))->symbole].taille	= taille;
		}
	}

	return;
}
